<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 17-2-10
 * Time: 下午6:12
 */

namespace Cframe;


class Lodder {
    protected static $allType = array();
    protected static $allFile = array();

    /**
     * @param $class 自动载入的
     * @return bool
     */
    public static function autoload($class){
        if ($file = static::findFile($class)){
            if (IS_WIN && pathinfo($file,PATHINFO_FILENAME) != pathinfo(realpath($file),PATHINFO_FILENAME)){
                return false;
            }
            if (!in_array($file,get_included_files())) {
                static::__include_file($file);
            }
        }
    }

    public static function findFile($class){
        $allClassContent = explode('\\',$class);
        $namespace = $allClassContent[0];
        $rootpath = isset(static::$allType[$namespace])?static::$allType[$namespace]:'';
        $allClassContent[0] = $rootpath;
        $filename = implode(DS,$allClassContent) . '.php';
        if (file_exists($filename)){
            return $filename;
        }
        return false;
    }

    public static function register(){
        register_shutdown_function([__CLASS__,'yfShutdown']);
        spl_autoload_register(__NAMESPACE__ . '\Lodder::autoload');
        static::autoRegister();
    }

    public static function yfShutdown(){
        if (!APP_DEBUG){
            if (class_exists(__NAMESPACE__."\\Log")){
                Log::save();
            }
            if (class_exists(__NAMESPACE__."\\Hook")){
                Hook::save();
            }
        }
    }

    private static function autoRegister(){
        $config = [];
        $config2 = [];
        if (file_exists(COMMON_PATH . 'Lodder.php')){
            $config = include COMMON_PATH . 'Lodder.php';
        }
        if (file_exists(CON_PATH . 'register.php')){
            $config2 = include CON_PATH . 'register.php';
        }
        if (!empty($config2) && is_array($config2)){
            $config = array_merge($config,$config2);
        }
        if (isset($config['path'])){
            static::addType($config['path']);
        }
        if (isset($config['file'])) {
            static::addFile($config['file']);
        }
    }

    private static function loadFile($file = ''){
        if (!empty($file)){
            if (is_array($file)){
                foreach ($file as $k){
                    static::__include_file($k);
                }
            } else {
                static::__include_file($file);
            }
        } else {
            foreach (static::$allFile as $k) {
                static::__include_file($k);
            }
        }
    }

    public static function addFile($file){
        if (is_array($file)){
            static::$allFile = array_merge(static::$allFile,$file);
        }else{
            array_push(static::$allFile,$file);
        }
        static::loadFile();
    }

    public static function addType(array $array = array()){
        static::$allType = array_merge(static::$allType,$array);
    }

    public static function __include_file($file){
        if (file_exists($file)){
            include $file;
        }
    }
}